package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.CategoryVariableAddDTO;
import org.jsola.contract.dto.CategoryVariableUpdateDTO;
import org.jsola.contract.query.CategoryVariableQuery;
import org.jsola.contract.service.ICategoryVariableService;
import org.jsola.contract.vo.CategoryVariableListVO;
import org.jsola.contract.vo.CategoryVariableVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同分类下模板变量
 *
 * @author JanusMix
 */
@RestController("contractCategoryVariableController")
@RequestMapping(ContractConstants.API_PATH + "/v1/categoryVariables")
public class CategoryVariableController implements ICategoryVariableControllerAPI {

    @Autowired
    private ICategoryVariableService categoryVariableService;

    @Override
    @PostMapping
    public Result<CategoryVariableVO> save(@Valid @RequestBody CategoryVariableAddDTO categoryVariableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同分类下模板变量权限校验

        return Result.success(
                categoryVariableService.save(categoryVariableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CategoryVariableUpdateDTO categoryVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同分类下模板变量权限校验

        int count = categoryVariableService.update(categoryVariableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{categoryVariableId}")
    public Result<Integer> delete(@PathVariable Long categoryVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同分类下模板变量权限校验

        int count = categoryVariableService.deleteByIds(tokenUser, categoryVariableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] categoryVariableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同分类下模板变量权限校验

        int count = categoryVariableService.deleteByIds(tokenUser, categoryVariableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{categoryVariableId}")
    public Result<CategoryVariableVO> findById(@PathVariable Long categoryVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同分类下模板变量权限校验

        return Result.success(
            categoryVariableService.selectById(categoryVariableId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CategoryVariableListVO>> listAll(@Valid CategoryVariableQuery categoryVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同分类下模板变量权限校验

        return Result.success(
            categoryVariableService.select(categoryVariableQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CategoryVariableListVO>> page(@Valid CategoryVariableQuery categoryVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            categoryVariableService.selectPage(categoryVariableQuery, tokenUser.getSiteId())
        );
    }


}

